package com.hxl.tech.gateway.auth.dto;


import com.hxl.tech.gateway.common.constant.Result;
import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {

    int code;

    String msg;

    T data;

    PageInfo pageInfo;

    public static Response createSuccess() {

        return builder().code(200).msg("success").build();
    }

    public static <T> Response<T> createSuccess(T data) {

        return builder().code(200).msg("success").result(data).build();
    }

    public static <T> Response<T> createSuccess(T data, PageInfo pageInfo) {

        return builder().code(200).msg("success").result(data).pageInfo(pageInfo).build();
    }

    public static <T> Response<T> createSuccess(Page page) {

        return builder().code(200).msg("success").result(page.getResult()).pageInfo(page.getPageInfo()).build();
    }

    public static Response createFail(String msg) {

        return builder().code(Result.FAIL.getCode()).msg(msg).build();
    }

    public static Response createFail(Integer code, String msg) {

        return builder().code(code).msg(msg).build();
    }

    public static Response createFail(Result result, String msg) {

        return builder().code(result.getCode()).msg( result.getMsg() + ":" +msg).build();
    }

    public static Builder builder() {
        return new Builder();
    }

    public Response() {
    }

    public String toString() {
        return "Response(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public static class Builder<T> {
        int code;
        String msg;
        T data;
        PageInfo pageInfo;

        public Builder() {
        }

        public Builder code(int code) {
            this.code = code;
            return this;
        }

        public Builder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder result(T data) {
            this.data = data;
            return this;
        }

        public Builder pageInfo(PageInfo pageInfo) {
            this.pageInfo = pageInfo;
            return this;
        }

        public Response build() {
            Response response = new Response();
            response.setCode(this.code);
            response.setMsg(this.msg);
            response.setData(this.data);
            response.setPageInfo(this.pageInfo);
            return response;
        }
    }

}
